* Clean up

clear all

set maxvar 10000

/*
* Set working directory: please set your own
cd "/Users/vicentevalentim/Desktop/fragmentation_replication_bjps"
*/

* Open dataset
use Data/MPDataset_MPDS2018b_stata14, clear

* Homogenize countryname for Western Germany
replace countryname = "Germany" if countryname == "German Democratic Republic"

** Adding variable for thresholds thresholds and data on it
gen threshold = .

replace threshold = 4 if countryname == "Albania"
replace threshold = 2 if countryname == "Albania" & date == 199706
replace threshold = 2.5 if countryname == "Albania" & date == 200106
replace threshold = . if countryname == "Albania" & date == 199103
replace threshold = 4 if countryname == "Albania" & date == 199203
replace threshold = 5 if countryname == "Armenia"
replace threshold = 3 if countryname == "South Korea"
replace threshold = 4 if countryname == "Austria" & date > 199400
replace threshold = 4 if countryname == "Bulgaria"
replace threshold = 5 if countryname == "Croatia"
replace threshold = 3 if countryname == "Croatia" & date == 199208
replace threshold = 1.79 if countryname == "Cyprus"
replace threshold = 5 if countryname == "Czech Republic"
replace threshold = 2 if country == 13 & date > 197100
replace threshold = 5 if countryname == "Estonia"
replace threshold = 7 if countryname == "Georgia" & date == 200403
replace threshold = 5 if countryname == "Georgia" & date == 200805
replace threshold = 5 if countryname == "Germany" & date > 195300
replace threshold = 3 if countryname == "Greece" & date > 199300
replace threshold = 5 if countryname == "Hungary"
replace threshold = 4 if countryname == "Hungary" & date == 199003
replace threshold = 5 if countryname == "Iceland" & date > 200300
replace threshold = 1 if countryname == "Israel"
replace threshold = 0.83 if countryname == "Israel" & date == 194901
replace threshold = 1.5 if countryname == "Israel" & date > 199200
replace threshold = 2 if countryname == "Israel" & date > 200600
replace threshold = 3.25 if countryname == "Israel" & date == 201503
replace threshold = 4 if countryname == "Italy" & date > 199400
replace threshold = 4 if countryname == "Latvia" & date == 199306
replace threshold = 5 if countryname == "Latvia" & date > 199500
replace threshold = 4 if countryname == "Lithuania" & date == 199210
replace threshold = 5 if countryname == "Lithuania" & date > 199600
replace threshold = 5 if countryname == "Macedonia" & date > 199809
replace threshold = 4 if countryname == "Moldova" & date < 199900
replace threshold = 6 if countryname == "Moldova" & date > 199900 & date < 200900
replace threshold = 6 if countryname == "Moldova" & date == 200904
replace threshold = 5 if countryname == "Moldova" & date == 200907
replace threshold = 4 if countryname == "Moldova" & date == 201011
replace threshold = 4 if countryname == "Moldova" & date == 201411
replace threshold = 2 if countryname == "Mexico" & date > 200000
replace threshold = 3 if countryname == "Mexico" & date > 201500
replace threshold = 3 if countryname == "Montenegro" & date > 200600

replace threshold = 1 if countryname == "Netherlands" & date < 195300
replace threshold = 0.67 if countryname == "Netherlands" & date > 195300
replace threshold = 4 if countryname == "Norway" & date > 198900
replace threshold = 5 if countryname == "New Zealand" & date > 199600
replace threshold = 5 if countryname == "Poland" & date > 199300
replace threshold = 3 if countryname == "Romania" & date > 199200
replace threshold = 5 if countryname == "Romania" & date > 200000
replace threshold = . if countryname == "Romania" & date > 200900 
replace threshold = 5 if countryname == "Russia" & date < 200700
replace threshold = 7 if countryname == "Russia" & date > 200700
replace threshold = 5 if countryname == "Slovakia"
replace threshold = 4 if countryname == "Sweden" & date > 197000
replace threshold = 10 if countryname == "Turkey" & date > 198300
replace threshold = 4 if countryname == "Ukraine" & date > 199800
replace threshold = 3 if countryname == "Ukraine" & date > 200600
replace threshold = 2 if countryname == "Denmark" & date > 197100
replace threshold = 3 if countryname == "South Korea"

 ta threshold if countryname == "Macedonia", mis
 ta date if countryname == "Macedonia" & threshold == .
* Generate dummy to indicate these elections do not come from the Dinas dataset
gen dinas = 0

* keep only parliamentary parties (others come from Dinas et al 2015)
keep if absseat > 0

* Save this dataset
save Data/cmp_partylevel1, replace

* Append with data from Dinas et al 2015 (updated to 2018): elections not in CMP
use Data/Dinasetal2015_updated2018.dta, clear

* Generate country-year variable
tostring year, replace force
gen country_year = countryname + year
destring year, replace
ta country_year
replace country_year = "Moldova2009.2" if country_year == "Moldova2009.199951"
replace country_year = "Moldova2009.1" if country_year == "Moldova2009.099976"

* Keep only elections that are not included in the CMP 
* (non-parliamentary parties for elections included in CMP will be appended later on)
* CHANGE HERE
keep if countryname == "Peru" | ///
countryname == "Seychelles" | countryname == "Bolivia" | ///
countryname == "Taiwan" | ///
country_year == "Moldova2009.1" | ///
country_year == "Ukraine2012" | ///
country_year == "Ukraine2014" | ///
countryname == "Taiwan" | ///
country_year == "Sweden2018" | ///
country_year == "Norway2017" | ///
country_year == "New Zealand2017" | ///
country_year == "Lithuania2016" | ///
country_year == "Latvia2018" | ///
country_year == "Armenia2017" | ///
country_year == "Albania2005" | ///
country_year == "Albania2009" | ///
country_year == "Albania2013" | ///
country_year == "Albania2017" | ///
country_year == "Denmark2015" | ///
country_year == "South Korea2018" | ///
country_year == "Taiwan2018" | ///
country_year == "Slovenia2018" | ///
country_year == "Slovenia2014" | ///
country_year == "Seychelles2016" | ///
country_year == "Seychelles2011" | ///
country_year == "Romania2018" | ///
country_year == "Norway2018" | ///
country_year == "NewZealand2018" | ///
country_year == "Moldova2018" | ///
country_year == "Lithuania2018" | ///
country_year == "Denmark2018" | ///
country_year == "Bolivia2018" | ///
country_year == "Armenia2018" | ///
country_year == "Italy2018" | ///
country_year == "Iceland2016" | ///
country_year == "Iceland2017" | ///
country_year == "Hungary2018" | ///
country_year == "Mexico2018" | ///
country_year == "South Korea1988"

* Generate dummy to indicate these data come from Dinas et al
gen dinas = 1

* Homogenize variable names
rename seats absseat
rename votes pervote
sort country_year

* Generate cntryyear variable
gen cntryyear = " "
replace cntryyear = "Bolivia1_1997" if country_year == "Bolivia1997"
replace cntryyear = "Bolivia2_2002" if country_year == "Bolivia2002"
replace cntryyear = "Bolivia3_2005" if country_year == "Bolivia2005"
replace cntryyear = "Bolivia4_2009" if country_year == "Bolivia2009"
replace cntryyear = "Peru1_2006" if country_year == "Peru2006"
replace cntryyear = "Peru2_2011" if country_year == "Peru2011"
replace cntryyear = "Peru3_2016" if country_year == "Peru2016"
replace cntryyear = "Seychelles1_1993" if country_year == "Seychelles1993"
replace cntryyear = "Seychelles2_1998" if country_year == "Seychelles1998"
replace cntryyear = "Seychelles3_2002" if country_year == "Seychelles2002"
replace cntryyear = "Seychelles4_2007" if country_year == "Seychelles2007"
replace cntryyear = "Slovenia1_1992" if country_year == "Slovenia1992"
replace cntryyear = "Slovenia2_1996" if country_year == "Slovenia1996"
replace cntryyear = "Slovenia3_2000" if country_year == "Slovenia2000"
replace cntryyear = "Slovenia4_2004" if country_year == "Slovenia2004"
replace cntryyear = "Slovenia5_2008" if country_year == "Slovenia2008"
replace cntryyear = "Slovenia6_2011" if country_year == "Slovenia2011"
replace cntryyear = "Slovenia7_2014" if country_year == "Slovenia2014"
replace cntryyear = "Slovenia8_2018" if country_year == "Slovenia2018"
replace cntryyear = "Taiwan1_1992" if country_year == "Taiwan1992"
replace cntryyear = "Taiwan2_1995" if country_year == "Taiwan1995"
replace cntryyear = "Taiwan3_1998" if country_year == "Taiwan1998"
replace cntryyear = "Taiwan4_2001" if country_year == "Taiwan2001"
replace cntryyear = "Taiwan5_2004" if country_year == "Taiwan2004"
replace cntryyear = "Taiwan6_2008" if country_year == "Taiwan2008"
replace cntryyear = "Taiwan7_2012" if country_year == "Taiwan2012"
replace cntryyear = "Taiwan8_2016" if country_year == "Taiwan2016"
replace cntryyear = "Moldova1_2009" if country_year == "Moldova2009.1"
replace cntryyear = "Ukraine1_2012" if country_year == "Ukraine2012"
replace cntryyear = "Ukraine2_2014" if country_year == "Ukraine2014"
replace cntryyear = "Sweden2018" if country_year == "Sweden2018"
replace cntryyear = "Romania2012" if country_year == "Romania2012"
replace cntryyear = "Romania2016" if country_year == "Romania2016"
replace cntryyear = "Norway2017" if country_year == "Norway2017"
replace cntryyear = "New Zealand2017" if country_year == "New Zealand2017"
replace cntryyear = "Moldova2014" if country_year == "Moldova2014"
replace cntryyear = "Lithuania2016" if country_year == "Lithuania2016"
replace cntryyear = "Latvia2018" if country_year == "Latvia2018"
replace cntryyear = "Armenia2017" if country_year == "Armenia2017"
replace cntryyear = "South Korea1988" if country_year == "South Korea1988"
replace cntryyear = "Seychelles2016" if country_year == "Seychelles2016"
replace cntryyear = "Denmark2015" if country_year == "Denmark2015"
replace cntryyear = "Albania2017" if country_year == "Albania2017"
replace cntryyear = "Albania2013" if country_year == "Albania2013"
replace cntryyear = "Albania2009" if country_year == "Albania2009"
replace cntryyear = "Albania2005" if country_year == "Albania2005"
replace cntryyear = "Italy2018" if country_year == "Italy2018"
replace cntryyear = "Iceland2016" if country_year == "Iceland2016"
replace cntryyear = "Iceland2017" if country_year == "Iceland2017"
replace cntryyear = "Hungary2018" if country_year == "Hungary2018"
replace cntryyear = "Seychelles2011" if country_year == "Seychelles2011"
replace cntryyear = "Mexico2018" if country_year == "Mexico2018"

ta country_year
ta country_year if cntryyear == " " // No observations, which is what we want

* Generate date variable
gen date = .

replace date = 200607 if cntryyear == "Albania2005"
replace date = 200906 if cntryyear == "Albania2009"
replace date = 201306 if cntryyear == "Albania2013"
replace date = 201706 if cntryyear == "Albania2017"
replace date = 199706 if cntryyear == "Bolivia1_1997"
replace date = 200206 if cntryyear == "Bolivia2_2002"
replace date = 200512 if cntryyear == "Bolivia3_2005"
replace date = 200912 if cntryyear == "Bolivia4_2009"
replace date = 200604 if cntryyear == "Peru1_2006"
replace date = 201104 if cntryyear == "Peru2_2011"
replace date = 201604 if cntryyear == "Peru3_2016" 
replace date = 199307 if country_year == "Seychelles1993"
replace date = 199803 if country_year == "Seychelles1998"
replace date = 200212 if country_year == "Seychelles2002"
replace date = 200705 if country_year == "Seychelles2007"
replace date = 199212 if cntryyear == "Slovenia1_1992"
replace date = 199611 if cntryyear == "Slovenia2_1996"
replace date = 200010 if cntryyear == "Slovenia3_2000"
replace date = 200410 if cntryyear == "Slovenia4_2004"
replace date = 200809 if cntryyear == "Slovenia5_2008"
replace date = 201112 if cntryyear == "Slovenia6_2011"
replace date = 201407 if cntryyear == "Slovenia7_2014"
replace date = 201806 if cntryyear == "Slovenia8_2018"
replace date = 199212 if cntryyear == "Taiwan1_1992"
replace date = 199512 if cntryyear == "Taiwan2_1995"
replace date = 199812 if cntryyear == "Taiwan3_1998"
replace date = 200112 if cntryyear == "Taiwan4_2001"
replace date = 200412 if cntryyear == "Taiwan5_2004"
replace date = 200904 if cntryyear == "Moldova1_2009"
replace date = 201210 if cntryyear == "Ukraine1_2012"
replace date = 201410 if cntryyear == "Ukraine2_2014"
replace date = 201809 if cntryyear == "Sweden2018"
replace date = 201212 if cntryyear == "Romania2012"
replace date = 201612 if cntryyear == "Romania2016"
replace date = 201709 if cntryyear == "Norway2017"
replace date = 201709 if cntryyear == "New Zealand2017"
replace date = 201411 if cntryyear == "Moldova2014"
replace date = 201610 if cntryyear == "Lithuania2016"
replace date = 201810 if cntryyear == "Latvia2018"
replace date = 201704 if cntryyear == "Armenia2017"
replace date = 201506 if cntryyear == "Denmark2015"
replace date = 201204 if cntryyear == "South Korea2012"
replace date = 201609 if cntryyear == "Seychelles2016"
replace date = 198804 if cntryyear == "South Korea1988"
replace date = 200801 if cntryyear == "Taiwan6_2008"
replace date = 201201 if cntryyear == "Taiwan7_2012"
replace date = 201601 if cntryyear == "Taiwan8_2016"
replace date = 201803 if cntryyear == "Italy2018"
replace date = 201610 if cntryyear == "Iceland2016"
replace date = 201710 if cntryyear == "Iceland2017"
replace date = 201804 if cntryyear == "Hungary2018"
replace date = 201109 if cntryyear == "Seychelles2011"
replace date = 201109 if cntryyear == "Seychelles2011"
replace date = 201807 if cntryyear == "Mexico2018"

ta cntryyear if date == . // No observations, which is what we want

* Add missing thresholds
ta date if countryname == "Peru"
replace threshold = 4 if countryname == "Peru" & date == 200604
replace threshold = 5 if countryname == "Peru" & date == 201104
replace threshold = 5 if countryname == "Peru" & date == 201604 // source https://www.eods.eu/library/eueom_peru_final_report_eng_dv_150716_pdf.pdf

ta date if countryname == "Taiwan"
replace threshold = 5 if countryname == "Taiwan" & date == 199212
replace threshold = 5 if countryname == "Taiwan" & date == 199512
replace threshold = 5 if countryname == "Taiwan" & date == 199812
replace threshold = 5 if countryname == "Taiwan" & date == 200112
replace threshold = 5 if countryname == "Taiwan" & date == 200412

ta date if countryname == "Moldova"
replace threshold = 6 if countryname == "Moldova" & date == 200904

replace threshold = 4 if countryname == "Slovenia" & date == 200010
replace threshold = 4 if countryname == "Slovenia" & date == 200410
replace threshold = 4 if countryname == "Slovenia" & date == 200809
replace threshold = 4 if countryname == "Slovenia" & date == 201112

ta date if countryname == "Georgia"
replace threshold = 7 if countryname == "Georgia" & date == 200403
replace threshold = 5 if countryname == "Georgia" & date == 200805
replace threshold = 5 if countryname == "Georgia" & date == 201210
replace threshold = 5 if countryname == "Georgia" & date == 201610

ta date if countryname == "Macedonia"
replace threshold = 5 if countryname == "Macedonia" & date == 199810
replace threshold = 5 if countryname == "Macedonia" & date == 200209
replace threshold = 5 if countryname == "Macedonia" & date == 200607

ta date if countryname == "Romania"
replace threshold = 3 if countryname == "Romania" & date == 199209
replace threshold = 3 if countryname == "Romania" & date == 199611
replace threshold = 5 if countryname == "Romania" & date == 200011
replace threshold = 5 if countryname == "Romania" & date == 200411
replace threshold = 5 if countryname == "Romania" & date == 200811
replace threshold = 5 if countryname == "Romania" & date == 201212
replace threshold = 5 if countryname == "Romania" & date == 201612

ta date if countryname == "Slovenia"
replace threshold = 4 if countryname == "Slovenia" & date == 200010
replace threshold = 4 if countryname == "Slovenia" & date == 200410
replace threshold = 4 if countryname == "Slovenia" & date == 200809
replace threshold = 4 if countryname == "Slovenia" & date == 201112

ta date if countryname == "Slovenia"
replace threshold = 3 if countryname == "Bolivia" & date == 200010


replace threshold = 3 if cntryyear == "Albania2009"
ta threshold year if countryname == "Albania"

ta cntryyear if threshold == . // No observations, which is what we want

drop country

* Keep only parliamentary parties (the remaining ones will be appended below)
keep if treat == 1

* Append with CMP data, so that we these additional elections

append using Data/cmp_partylevel1, force

save Data/cmp_partylevel2, replace


* Appending with data from Dinas et al 2015 (updated to 2018): non-parliamentary parties in elections in CMP
use Data/Dinasetal2015_updated2018.dta, clear

* Generate countrynameyear variable
tostring year, replace force
gen countrynameyear = countryname + year
destring year, replace

ta countrynameyear
* Keep only non-parliamentary parties, because parliamentary parties come from CMP
keep if seats == 0


* Generate dummy saying these data come from Dinas et al dataset
gen dinas = 1

* Homogenize variable names
rename seats absseat
rename votes pervote

append using Data/cmp_partylevel2, force

sort country_year

* Fixing dates in Dinas et al data
ta countryname year
ta countrynameyear if date == . 
ta year
sort countryname year
by countryname year: egen date_max = max(date)
ta date_max, mis
replace date = date_max if dinas == 1

* Replacing the elections with missing date, by hand
replace date = 199203 if countrynameyear == "Albania1992"
replace date = 199706 if countrynameyear == "Albania1997"
replace date = 200106 if countrynameyear == "Albania2001"
replace date = 200507 if countrynameyear == "Albania2005"
replace date = 200906 if countrynameyear == "Albania2009"
replace date = 201306 if countrynameyear == "Albania2013"
replace date = 201706 if countrynameyear == "Albania2017"
replace date = 199507 if countrynameyear == "Armenia1995"
replace date = 199905 if countrynameyear == "Armenia1999"
replace date = 200305 if countrynameyear == "Armenia2003"
replace date = 200705 if countrynameyear == "Armenia2007"
replace date = 201205 if countrynameyear == "Armenia2012"
replace date = 201704 if countrynameyear == "Armenia2017"
replace date = 199910 if countrynameyear == "Austria1999"
replace date = 200610 if countrynameyear == "Austria2006"
replace date = 201309 if countrynameyear == "Austria2013"
replace date = 201710 if countrynameyear == "Austria2017"
replace date = 200206 if countrynameyear == "Bolivia2002"
replace date = 200512 if countrynameyear == "Bolivia2005"
replace date = 200912 if countrynameyear == "Bolivia2009"
replace date = 199006 if countrynameyear == "Bulgaria1990"
replace date = 199110 if countrynameyear == "Bulgaria1991"
replace date = 199704 if countrynameyear == "Bulgaria1997"
replace date = 200106 if countrynameyear == "Bulgaria2001"
replace date = 200506 if countrynameyear == "Bulgaria2005"
replace date = 200907 if countrynameyear == "Bulgaria2009"
replace date = 201305 if countrynameyear == "Bulgaria2013"
replace date = 201410 if countrynameyear == "Bulgaria2014"
replace date = 201703 if countrynameyear == "Bulgaria2017"
replace date = 199605 if countrynameyear == "Cyprus1996"
replace date = 200605 if countrynameyear == "Cyprus2006"
replace date = 201105 if countrynameyear == "Cyprus2011"
replace date = 201605 if countrynameyear == "Cyprus2016"
replace date = 200001 if countrynameyear == "Croatia2000"
replace date = 201112 if countrynameyear == "Croatia2011"
replace date = 201511 if countrynameyear == "Croatia2015"
replace date = 201609 if countrynameyear == "Croatia2016"
replace date = 199006 if countrynameyear == "Czech Republic1990"
replace date = 199206 if countrynameyear == "Czech Republic1992"
replace date = 200606 if countrynameyear == "Czech Republic2006"
replace date = 201310 if countrynameyear == "Czech Republic2013"
replace date = 201710 if countrynameyear == "Czech Republic2017"
replace date = 198709 if countrynameyear == "Denmark1987"
replace date = 198805 if countrynameyear == "Denmark1988"
replace date = 199012 if countrynameyear == "Denmark1990"
replace date = 200111 if countrynameyear == "Denmark2001"
replace date = 200711 if countrynameyear == "Denmark2007"
replace date = 201109 if countrynameyear == "Denmark2011"
replace date = 201506 if countrynameyear == "Denmark2015"
replace date = 199209 if countrynameyear == "Estonia1992"
replace date = 199503 if countrynameyear == "Estonia1995"
replace date = 200303 if countrynameyear == "Estonia2003"
replace date = 200703 if countrynameyear == "Estonia2007"
replace date = 201503 if countrynameyear == "Estonia2015"
replace date = 200403 if countrynameyear == "Georgia2004"
replace date = 200805 if countrynameyear == "Georgia2008"
replace date = 196109 if countrynameyear == "Germany1961"
replace date = 196509 if countrynameyear == "Germany1965"
replace date = 196909 if countrynameyear == "Germany1969"
replace date = 197211 if countrynameyear == "Germany1972"
replace date = 197610 if countrynameyear == "Germany1976"
replace date = 198010 if countrynameyear == "Germany1980"
replace date = 198303 if countrynameyear == "Germany1983"
replace date = 198701 if countrynameyear == "Germany1987"
replace date = 199012 if countrynameyear == "Germany1990"
replace date = 199410 if countrynameyear == "Germany1994"
replace date = 199809 if countrynameyear == "Germany1998"
replace date = 200209 if countrynameyear == "Germany2002"
replace date = 200509 if countrynameyear == "Germany2005"
replace date = 200909 if countrynameyear == "Germany2009"
replace date = 201709 if countrynameyear == "Germany2017"
replace date = 199310 if countrynameyear == "Greece1993"
replace date = 200004 if countrynameyear == "Greece2000"
replace date = 200403 if countrynameyear == "Greece2004"
replace date = 200709 if countrynameyear == "Greece2007"
replace date = 200910 if countrynameyear == "Greece2009"
replace date = 201205 if countrynameyear == "Greece2012.099976"
replace date = 201206 if countrynameyear == "Greece2012.199951"
replace date = 201501 if countrynameyear == "Greece2015.099976"
replace date = 201509 if countrynameyear == "Greece2015.199951"  
replace date = 200604 if countrynameyear == "Hungary2006"
replace date = 201004 if countrynameyear == "Hungary2010"
replace date = 201404 if countrynameyear == "Hungary2014"
replace date = 201804 if countrynameyear == "Hungary2018"
replace date = 200305 if countrynameyear == "Iceland2003"
replace date = 200705 if countrynameyear == "Iceland2007"
replace date = 200904 if countrynameyear == "Iceland2009"
replace date = 201304 if countrynameyear == "Iceland2013"
replace date = 201610 if countrynameyear == "Iceland2016"
replace date = 201710 if countrynameyear == "Iceland2017"
replace date = 195507 if countrynameyear == "Israel1955"
replace date = 195907 if countrynameyear == "Israel1959"
replace date = 196108 if countrynameyear == "Israel1961"
replace date = 197312 if countrynameyear == "Israel1973"
replace date = 197705 if countrynameyear == "Israel1977"
replace date = 198407 if countrynameyear == "Israel1984"
replace date = 198811 if countrynameyear == "Israel1988"
replace date = 199605 if countrynameyear == "Israel1996"
replace date = 199905 if countrynameyear == "Israel1999"
replace date = 200301 if countrynameyear == "Israel2003"
replace date = 200603 if countrynameyear == "Israel2006"
replace date = 200902 if countrynameyear == "Israel2009"
replace date = 201301 if countrynameyear == "Israel2013"
replace date = 201503 if countrynameyear == "Israel2015"
replace date = 199403 if countrynameyear == "Italy1994"
replace date = 200604 if countrynameyear == "Italy2006"
replace date = 200804 if countrynameyear == "Italy2008"
replace date = 201302 if countrynameyear == "Italy2013"
replace date = 199810 if countrynameyear == "Latvia1998"
replace date = 200610 if countrynameyear == "Latvia2006"
replace date = 201010 if countrynameyear == "Latvia2010"
replace date = 201109 if countrynameyear == "Latvia2011"
replace date = 201410 if countrynameyear == "Latvia2014"
replace date = 201810 if countrynameyear == "Latvia2018"
replace date = 200010 if countrynameyear == "Lithuania2000"
replace date = 200410 if countrynameyear == "Lithuania2004"
replace date = 200810 if countrynameyear == "Lithuania2008"
replace date = 201210 if countrynameyear == "Lithuania2012"
replace date = 201610 if countrynameyear == "Lithuania2016"
replace date = 199810 if countrynameyear == "Macedonia1998"
replace date = 200007 if countrynameyear == "Mexico2000"
replace date = 200306 if countrynameyear == "Mexico2003"
replace date = 201506 if countrynameyear == "Mexico2015"
replace date = 199402 if countrynameyear == "Moldova1994"
replace date = 199803 if countrynameyear == "Moldova1998"
replace date = 200102 if countrynameyear == "Moldova2001"
replace date = 200503 if countrynameyear == "Moldova2005"
replace date = 200904 if countrynameyear == "Moldova2009.099976"
replace date = 200907 if countrynameyear == "Moldova2009.199951"
replace date = 201011 if countrynameyear == "Moldova2010"
replace date = 201411 if countrynameyear == "Moldova2014"
replace date = 200903 if countrynameyear == "Montenegro2009"
replace date = 201210 if countrynameyear == "Montenegro2012"
replace date = 201610 if countrynameyear == "Montenegro2012"
replace date = 194605 if countrynameyear == "Netherlands1946"
replace date = 194807 if countrynameyear == "Netherlands1948"
replace date = 195206 if countrynameyear == "Netherlands1952"
replace date = 195606 if countrynameyear == "Netherlands1956"
replace date = 195903 if countrynameyear == "Netherlands1959"
replace date = 196305 if countrynameyear == "Netherlands1963"
replace date = 196702 if countrynameyear == "Netherlands1967"
replace date = 197104 if countrynameyear == "Netherlands1971"
replace date = 197211 if countrynameyear == "Netherlands1972"
replace date = 197705 if countrynameyear == "Netherlands1977"
replace date = 198105 if countrynameyear == "Netherlands1981"
replace date = 198209 if countrynameyear == "Netherlands1982"
replace date = 198605 if countrynameyear == "Netherlands1986"
replace date = 198909 if countrynameyear == "Netherlands1989"
replace date = 199405 if countrynameyear == "Netherlands1994"
replace date = 199805 if countrynameyear == "Netherlands1998"
replace date = 200205 if countrynameyear == "Netherlands2002"
replace date = 200611 if countrynameyear == "Netherlands2006"
replace date = 201006 if countrynameyear == "Netherlands2010"
replace date = 201209 if countrynameyear == "Netherlands2012"
replace date = 201703 if countrynameyear == "Netherlands2017"
replace date = 199610 if countrynameyear == "New Zealand1996"
replace date = 199911 if countrynameyear == "New Zealand1999"
replace date = 200207 if countrynameyear == "New Zealand2002"
replace date = 200509 if countrynameyear == "New Zealand2005"
replace date = 200811 if countrynameyear == "New Zealand2008"
replace date = 201111 if countrynameyear == "New Zealand2011"
replace date = 201409 if countrynameyear == "New Zealand2014"
replace date = 201709 if countrynameyear == "New Zealand2017"
replace date = 199309 if countrynameyear == "Norway1993"
replace date = 199709 if countrynameyear == "Norway1997"
replace date = 200109 if countrynameyear == "Norway2001"
replace date = 200509 if countrynameyear == "Norway2005"
replace date = 200909 if countrynameyear == "Norway2009"
replace date = 201309 if countrynameyear == "Norway2013"
replace date = 201709 if countrynameyear == "Norway2017"
replace date = 200604 if countrynameyear == "Peru2006"
replace date = 201104 if countrynameyear == "Peru2011"
replace date = 201604 if countrynameyear == "Peru2016"
replace date = 199309 if countrynameyear == "Poland1993"
replace date = 200109 if countrynameyear == "Poland2001"
replace date = 200509 if countrynameyear == "Poland2005"
replace date = 200710 if countrynameyear == "Poland2007"
replace date = 201110 if countrynameyear == "Poland2011"
replace date = 199611 if countrynameyear == "Romania1996"
replace date = 200011 if countrynameyear == "Romania2000"
replace date = 200411 if countrynameyear == "Romania2004"
replace date = 200811 if countrynameyear == "Romania2008"
replace date = 201212 if countrynameyear == "Romania2012"
replace date = 201612 if countrynameyear == "Romania2016"
replace date = 199512 if countrynameyear == "Russia1995"
replace date = 200312 if countrynameyear == "Russia2003"
replace date = 200712 if countrynameyear == "Russia2007"
replace date = 200212 if countrynameyear == "Seychelles2002"
replace date = 200705 if countrynameyear == "Seychelles2007"
replace date = 199006 if countrynameyear == "Slovakia1990"
replace date = 199206 if countrynameyear == "Slovakia1992"
replace date = 199409 if countrynameyear == "Slovakia1994"
replace date = 199809 if countrynameyear == "Slovakia1998"
replace date = 200606 if countrynameyear == "Slovakia2006"
replace date = 201203 if countrynameyear == "Slovakia2012"
replace date = 201603 if countrynameyear == "Slovakia2016"
replace date = 197009 if countrynameyear == "Sweden1970"
replace date = 197309 if countrynameyear == "Sweden1973"
replace date = 197609 if countrynameyear == "Sweden1976"
replace date = 197909 if countrynameyear == "Sweden1979"
replace date = 198209 if countrynameyear == "Sweden1982"
replace date = 198809 if countrynameyear == "Sweden1988"
replace date = 199409 if countrynameyear == "Sweden1994"
replace date = 199809 if countrynameyear == "Sweden1998"
replace date = 200209 if countrynameyear == "Sweden2002"
replace date = 200609 if countrynameyear == "Sweden2006"
replace date = 201409 if countrynameyear == "Sweden2014"
replace date = 201809 if countrynameyear == "Sweden2018"
replace date = 199512 if countrynameyear == "Turkey1995"
replace date = 200211 if countrynameyear == "Turkey2002"
replace date = 199803 if countrynameyear == "Ukraine1998"
replace date = 200203 if countrynameyear == "Ukraine2002"
replace date = 200603 if countrynameyear == "Ukraine2006"
replace date = 201210 if countrynameyear == "Ukraine2012"
replace date = 201410 if countrynameyear == "Ukraine2014"
replace date = 199512 if countrynameyear == "Taiwan1995"
replace date = 199812 if countrynameyear == "Taiwan1998"
replace date = 200112 if countrynameyear == "Taiwan2001"
replace date = 200412 if countrynameyear == "Taiwan2004"
replace date = 200801 if countrynameyear == "Taiwan2008"
replace date = 201201 if countrynameyear == "Taiwan2012"
replace date = 201601 if countrynameyear == "Taiwan2016"
replace date = 199605 if countrynameyear == "Albania1996"
replace date = 199410 if countrynameyear == "Austria1994"
replace date = 200211 if countrynameyear == "Austria2002"
replace date = 200809 if countrynameyear == "Austria2008"
replace date = 199706 if countrynameyear == "Bolivia1997"
replace date = 199412 if countrynameyear == "Bulgaria1994"
replace date = 199208 if countrynameyear == "Croatia1992"
replace date = 199510 if countrynameyear == "Croatia1995"
replace date = 199605 if countrynameyear == "Czech Republic1996"
replace date = 199806 if countrynameyear == "Czech Republic1998"
replace date = 200206 if countrynameyear == "Czech Republic2002"
replace date = 201005 if countrynameyear == "Czech Republic2010"
replace date = 197109 if countrynameyear == "Denmark1971"
replace date = 197312 if countrynameyear == "Denmark1973"
replace date = 197501 if countrynameyear == "Denmark1975"
replace date = 197910 if countrynameyear == "Denmark1979"
replace date = 198112 if countrynameyear == "Denmark1981"
replace date = 198401 if countrynameyear == "Denmark1984"
replace date = 199409 if countrynameyear == "Denmark1994"
replace date = 200502 if countrynameyear == "Denmark2005"
replace date = 199903 if countrynameyear == "Estonia1999"
replace date = 201103 if countrynameyear == "Estonia2011"
replace date = 201210 if countrynameyear == "Georgia2012"
replace date = 201610 if countrynameyear == "Georgia2016"
replace date = 194908 if countrynameyear == "Germany1949"
replace date = 195309 if countrynameyear == "Germany1953"
replace date = 195709 if countrynameyear == "Germany1957"
replace date = 199609 if countrynameyear == "Greece1996"
replace date = 199003 if countrynameyear == "Hungary1990"
replace date = 199405 if countrynameyear == "Hungary1994"
replace date = 199805 if countrynameyear == "Hungary1998"
replace date = 200204 if countrynameyear == "Hungary2002"
replace date = 198106 if countrynameyear == "Israel1981"
replace date = 199206 if countrynameyear == "Israel1992"
replace date = 199604 if countrynameyear == "Italy1996"
replace date = 200105 if countrynameyear == "Italy2001"
replace date = 201803 if countrynameyear == "Italy2018"
replace date = 199306 if countrynameyear == "Latvia1993"
replace date = 199509 if countrynameyear == "Latvia1995"
replace date = 199509 if countrynameyear == "Latvia1995"
replace date = 199210 if countrynameyear == "Lithuania1992"
replace date = 199610 if countrynameyear == "Lithuania1996"
replace date = 200209 if countrynameyear == "Macedonia2002"
replace date = 200607 if countrynameyear == "Macedonia2006"
replace date = 200806 if countrynameyear == "Macedonia2008"
replace date = 201106 if countrynameyear == "Macedonia2011"
replace date = 200907 if countrynameyear == "Mexico2009"
replace date = 200301 if countrynameyear == "Netherlands2003"
replace date = 198909 if countrynameyear == "Norway1989"
replace date = 199709 if countrynameyear == "Poland1997"
replace date = 199209 if countrynameyear == "Romania1992"
replace date = 199312 if countrynameyear == "Russia1993"
replace date = 199912 if countrynameyear == "Russia1999"
replace date = 201609 if countrynameyear == "Seychelles2016"
replace date = 200209 if countrynameyear == "Slovakia2002"
replace date = 201006 if countrynameyear == "Slovakia2010"
replace date = 199212 if countrynameyear == "Slovenia1992"
replace date = 199611 if countrynameyear == "Slovenia1996"
replace date = 200010 if countrynameyear == "Slovenia2000"
replace date = 200410 if countrynameyear == "Slovenia2004"
replace date = 200809 if countrynameyear == "Slovenia2008"
replace date = 201112 if countrynameyear == "Slovenia2011"
replace date = 201407 if countrynameyear == "Slovenia2014"
replace date = 201806 if countrynameyear == "Slovenia2018"
replace date = 198804 if countrynameyear == "South Korea1988"
replace date = 199203 if countrynameyear == "South Korea1992"
replace date = 199604 if countrynameyear == "South Korea1996"
replace date = 200004 if countrynameyear == "South Korea2000"
replace date = 200404 if countrynameyear == "South Korea2004"
replace date = 200804 if countrynameyear == "South Korea2008"
replace date = 198509 if countrynameyear == "Sweden1985"
replace date = 199109 if countrynameyear == "Sweden1991"
replace date = 198711 if countrynameyear == "Turkey1987"
replace date = 199904 if countrynameyear == "Turkey1999"
replace date = 200707 if countrynameyear == "Turkey2007"
replace date = 201106 if countrynameyear == "Turkey2011"
replace date = 200709 if countrynameyear == "Ukraine2007"
replace date = 200210 if countrynameyear == "Latvia2002"
replace date = 200311 if countrynameyear == "Croatia2003"
replace date = 200711 if countrynameyear == "Croatia2007"
replace date = 201309 if countrynameyear == "Germany2013"
replace date = 194901 if countrynameyear == "Israel1949"
replace date = 195107 if countrynameyear == "Israel1951"
replace date = 195507 if countrynameyear == "Israel1955"
replace date = 195907 if countrynameyear == "Israel1959"
replace date = 196108 if countrynameyear == "Israel1961"
replace date = 196511 if countrynameyear == "Israel1965"
replace date = 196910 if countrynameyear == "Israel1969"
replace date = 197312 if countrynameyear == "Israel1973"
replace date = 197705 if countrynameyear == "Israel1977"
replace date = 198106 if countrynameyear == "Israel1981"
replace date = 198407 if countrynameyear == "Israel1984"
replace date = 198811 if countrynameyear == "Israel1988"
replace date = 199206 if countrynameyear == "Israel1992"
replace date = 199605 if countrynameyear == "Israel1996"
replace date = 200301 if countrynameyear == "Israel2003"
replace date = 200603 if countrynameyear == "Israel2006"
replace date = 200902 if countrynameyear == "Israel2009"
replace date = 199204 if countrynameyear == "Italy1992"
replace date = 199110 if countrynameyear == "Turkey1991"
replace date = 200507 if countrynameyear == "Albania2005"
replace date = 200805 if countrynameyear == "Georgia2008"
replace date = 199512 if countrynameyear == "Austria1995"
replace date = 197702 if countrynameyear == "Denmark1977"
replace date = 199803 if countrynameyear == "Denmark1998"
replace date = 200609 if countrynameyear == "Montenegro2006"
replace date = 201610 if countrynameyear == "Montenegro2016"
replace date = 201112 if countrynameyear == "Russia2011"
replace date = 201204 if countrynameyear == "South Korea2012"
replace date = 201604 if countrynameyear == "South Korea2016"
replace date = 201009 if countrynameyear == "Sweden2010"
replace date = 199212 if countrynameyear == "Taiwan1992"
replace date = 201511 if countrynameyear == "Turkey2015"

replace date = 201210 if countryname == "Montenegro" & year == 2012
replace date = 201610 if countryname == "Montenegro" & year == 2016
replace date = 201506 if countryname == "Turkey" & year == 2015 & edate == "07jun2015"
replace date = 201511 if countryname == "Turkey" & year == 2015 & edate == "01nov2015"
replace date = 201609 if countryname == "Croatia" & year == 2016
replace date = 201511 if countryname == "Croatia" & year == 2015
replace date = 200507 if countryname == "Albania" & year == 2005
replace date = 200906 if countryname == "Albania" & year == 2009
replace date = 201306 if countryname == "Albania" & year == 2013
replace date = 201706 if countryname == "Albania" & year == 2017
replace date = 199512 if countryname == "Austria" & year == 1995
replace date = 201609 if countryname == "Seychelles" & year == 2016
replace date = 200609 if countryname == "Montenegro" & year == 2006
replace date = 201112 if countryname == "Russia" & year == 2011
replace date = 201009 if countryname == "Sweden" & year == 2010
replace date = 200801 if countryname == "Taiwan" & year == 2008
replace date = 201011 if countryname == "Taiwan" & year == 2010
replace date = 201601 if countryname == "Taiwan" & year == 2016
replace date = 197702 if countryname == "Denmark" & year == 1977
replace date = 199803 if countryname == "Denmark" & year == 1998
replace date = 201604 if countryname == "South Korea" & year == 2016

* Drop Armenia 2013 (presidential election)
drop if countrynameyear == "Armenia2013"

ta countryname if date == .

ta year if date == . & countryname == "Taiwan"

* Add missing thresholds
* check which elections have no threshold
ta countryname if threshold == .

* add them if they have one
replace threshold = 4 if countryname == "Moldova"  & date == 201411
replace threshold = 4 if countryname == "Moldova"  & date == 201011
replace threshold = 5  if date == 201210 & countryname == "Georgia" // source https://www.europarl.europa.eu/cmsdata/212639/Georgia-parliamentary-elections_1-October-2012_European-Parliament-report.pdf
replace threshold = 5  if date == 201610 & countryname == "Georgia" // source: https://www.electionguide.org/elections/id/2577/ 
replace threshold = 5 if date == 201804 & countrynameyear == "Hungary2018"
replace threshold = 5  if date == 201610 & countrynameyear == "Iceland2016"
replace threshold = 5  if date == 201710 & countrynameyear == "Iceland2016"
replace threshold = 5  if date == 201810 & countrynameyear == "Latvia2018"
replace threshold = 3  if year == 2009 & countryname == "Albania"

ta year if countryname == "Greece" & threshold == .
ta date if countryname == "Greece" & threshold == .
* these are all pre-1993, so all good

replace threshold = 5 if countryname == "Germany"

ta year if countryname == "Georgia" & threshold == .
* Georgia has a threshold of 5, source: https://www.europarl.europa.eu/cmsdata/212639/Georgia-parliamentary-elections_1-October-2012_European-Parliament-report.pdf
replace threshold = 5  if date == 201210 & countryname == "Georgia"
replace threshold = 5  if date == 201610 & countryname == "Georgia"
ta year if countryname == "Georgia" & threshold == .
* only Georgian elections left are the ones before threshold was introduced, so all good

ta year if countryname == "Iceland" & threshold == .
ta date if countryname == "Iceland" & threshold == .
* only Icelandic elections left are the ones before threshold was introduced, so all good

ta date if countryname == "Italy" & threshold == .
* only Italian elections left are the ones before threshold was introduced, so all good

ta date if countryname == "Macedonia" & threshold == .
replace threshold = 5 if countryname == "Macedonia"  & date == 199410
ta date if countryname == "Macedonia" & threshold == .
* only Italian elections left are the ones before and after threshold existed, so all good

ta date if countryname == "Mexico" & threshold == .

* only Mexican elections left are the ones before and after threshold existed, so all good

ta date if countryname == "Moldova" & threshold == .
replace threshold = 4 if countryname == "Moldova"  & date == 201411

ta date if countryname == "Montenegro" & threshold == .
* only Montenegro elections left are the ones before and after threshold existed, so all good

ta date if countryname == "New Zealand" & threshold == .
* only New Zealand elections left are the ones before and after threshold existed, so all good

ta date if countryname == "Norway" & threshold == .
* only Norway elections left are the ones before and after threshold existed, so all good

ta date if countryname == "Poland" & threshold == .
* only Poland elections left are the ones before and after threshold existed, so all good

ta date if countryname == "Romania" & threshold == .
replace threshold = 5 if countryname == "Romania"  & date == 201212
replace threshold = 5 if countryname == "Romania"  & date == 201612
ta date if countryname == "Romania" & threshold == .
* only Romania elections left are the ones before and after threshold existed, so all good

ta date if countryname == "Slovenia" & threshold == .
replace threshold = 4 if countryname == "Slovenia"  & date ==  200010
replace threshold = 4 if countryname == "Slovenia"  & date == 200410
replace threshold = 4 if countryname == "Slovenia"  & date == 200809
replace threshold = 4 if countryname == "Slovenia"  & date == 201112
ta date if countryname == "Slovenia" & threshold == .
* only Romania elections left are the ones before and after threshold existed, so all good

ta date if countryname == "Sweden" & threshold == .
* only Sweden elections left are the ones before and after threshold existed, so all good

ta date if countryname == "Turkey" & threshold == .
* only Turkey elections left are the ones before and after threshold existed, so all good

ta date if countryname == "Ukraine" & threshold == .
* only Ukraine elections left are the ones before and after threshold existed, so all good


* Drop elections without threshold
drop if threshold == .

ta countrynameyear if date == .

tostring date, replace
gen cntrydate = countryname + date
destring date, replace
ta cntrydate

* Drop election in Germany March 1990, which happened in East Germany
drop if cntrydate == "Germany199003"

* Correcting thresholds
replace threshold = 5 if cntrydate == "Poland199309"
replace threshold = 5 if cntrydate == "Lithuania200010"
replace threshold = 5 if cntrydate == "Poland200109"
replace threshold = 3 if cntrydate == "Slovakia199006"
replace threshold = 3 if countrynameyear == "Albania2009"
replace threshold = 2.5 if cntrydate == "Albania200507"

* Drop Macedonian elections after 1998, since there is no electoral threshold (cf https://www.electionguide.org/elections/id/1346/)
drop if cntrydate  == "Macedonia199410"
drop if cntrydate == "Macedonia200209"
drop if cntrydate == "Macedonia200607"
drop if cntrydate == "Macedonia200806"
drop if cntrydate == "Macedonia201106"
drop if cntrydate == "Macedonia201404"

* Drop Croatia 1990, because it had no threshold
drop if cntrydate  == "Croatia199004"

* Drop Slovenia before 2000, because it had no threshold
drop if cntrydate  == "Slovenia199212"
drop if cntrydate  == "Slovenia199611"

ta cntrydate

encode countryname, gen(country_code)

ta country_code, nolabel

forvalues x = 1/38{
ta cntrydate threshold if country_code == `x'
}

ta threshold, mis
ta countrynameyear if threshold == .

* Fixing treat variable
drop treat
gen treat = .
replace treat = 0 if absseat == 0
replace treat = 1 if absseat > 0

* Homogenizing threshold in Cyprus
replace threshold = 1.79 if countryname == "Cyprus"

** Further manipulations

** Dropping observations we are not using
ta countrynameyear if threshold == .
drop if threshold == .

destring date, replace

forvalues x = 1/38{
ta treat cntrydate if country_code == `x'
}

* Generating variable performance, indicating whether a party is above the threshold
gen performance = pervote - threshold
sum performance

* Generating a variable that has the vote share of each party as a proportion of the threshold
gen vote_as_prop_threshold = pervote / threshold

* Generating variable that tells us the distance between each party and the threshold in proportion
gen distance_threshold_prop = vote_as_prop_threshold - 1

* Fixing country variable
drop country
encode countryname, gen(country)

tostring date, replace
drop cntrydate
gen cntrydate = countryname + date
destring date, replace
ta cntrydate

* Fixing electoral threshold in Netherlands 1956
replace threshold = 0.67 if cntrydate == "Netherlands195606"

ta pervote if cntrydate == "Iceland201304"
ta treat if cntrydate == "Iceland"

ta cntrydate if date > 201200

* Generating variable indicating the seatshare of each party
bysort country date: egen sum_seats = total(absseat)
gen seatshare = absseat / sum_seats
ta absseat if seatshare == .
replace seatshare = 0 if absseat == 0
ta absseat if seatshare > 0


ta country if cntrydate == "Croatia201511"
forvalues x = 1/34{
ta cntrydate treat if country == `x'
}

* Drop parties that got 0% of the vote and won no seats
drop if pervote == 0 & absseat == 0

forvalues x = 1/38{
ta cntrydate threshold  if country == `x'
}

ta country, nolabel
forvalues x = 1/38{
ta cntrydate treat  if country == `x'
}

saveold Data/fragmentation_partylevel.dta, replace version(12)
